<?php
/**
 * @since 2012.01.23.
 * @author Karasz Tamas
 * @link http://binga.hu/
 * @package Sorsolás
 * ______ _____ _   _ _____  ___  
 * | ___ \_   _| \ | |  __ \/ _ \ 
 * | |_/ / | | |  \| | |  \/ /_\ \
 * | ___ \ | | | . ` | | __|  _  |
 * | |_/ /_| |_| |\  | |_\ \ | | |
 * \____/ \___/\_| \_/\____|_| |_/
 * 
 */

$page_title = "Nyertes sorsolása";

$page_text = "";

//emailküldés
if ($_POST['action']=="sendmail"){

	// végignézzük a felhasználókat
	foreach ($_POST['username'] as $postid => $postname) {
		
		if ($_POST['useremail'][$postid]<>""){
			// küldjük a levelet \o/
			
			$mailsend = mail(
				$_POST['useremail'][$postid],
				"Brendon nyereményjáték",
				$_POST['message'][$postid],
				'MIME-Version: 1.0' . "\r\n" .
				'Content-type: text/html; charset=utf-8' . "\r\n" .
				'From: info@brendon.hu' . "\r\n" .
			    'Reply-To: info@brendon.hu' . "\r\n" .
			    'X-Mailer: PHP/' . phpversion()
			);
			
			if ($mailsend){
				$elkuldtuk[] = $postname; // C:
			}else{
				$nemkuldtuk[] = $postname;
			}
			
		}else{
			// nem kaptunk fészbúktól email címet
			$nemkuldtuk[] = $postname; // :C
		}
	}
	
	if (!empty($elkuldtuk)){
		$page_text .= "
			<div class='ui-widget admin_message'>
				<div style='padding: 0 .7em;' class='ui-state-default ui-corner-all'> 
					<p>
						<span style='float: left; margin-right: .3em;' class='ui-icon ui-icon-notice'></span>
						Email üzenet sikeresen el lett küldve a következő nyerteseknek: ".implode(", ",$elkuldtuk)."
					</p>
				</div>
			</div>
		";
	}
	
	if (!empty($nemkuldtuk)){
		$page_text .= "
			<div class='ui-widget admin_message'>
				<div style='padding: 0 .7em;' class='ui-state-default ui-corner-all'> 
					<p>
						<span style='float: left; margin-right: .3em;' class='ui-icon ui-icon-error'></span>
						Email üzenetet nem sikerült elküldve a következő nyerteseknek, mert nem érhető el az email címük: ".implode(", ",$nemkuldtuk)."
					</p>
				</div>
			</div>
		";
	}
	
}elseif ($_POST['action']=="sorsolas"){
	// sorsolás
	
	$id = $sql->escape($_POST['test_id']);
	$count = (int)$_POST['user_count'];
	
	$sql->mysql_select("upload", "*","`upload-gallery` = ".$id." and `upload-nyert` = 0 and `upload-useremail`<>''","RAND()","0, ".$count,"`upload-nyert`");
		
	foreach ($sql->get_table() as $userid => $userrow) {

		//TODO: törölni ezt a bejegyzést
		//$sql->mysql_update("upload",array("upload-nyert"=>1),"upload-userid = ".$userrow['upload-userid']);
		
		$form_hidden[] = "
			<input type='hidden' name='username[".$userid."]' value='".$userrow['upload-username']."' />
			<input type='hidden' name='useremail[".$userid."]' value='".$userrow['upload-useremail']."' />
		";
		
		$form_message[] = "
			<div class='fieldset'>
				<div class='legend'>
					<span class='left title'>Üzenet a nyertesnek: ".$userrow['upload-username']."</span>
				</div>
					
				<div class='sendmessage'>
					<textarea name='message[".$userid."]' class='tinymce'></textarea>
				</div>
			</div>
		";
	}
	
	$page_text .= "
		<form method='post' action='index.php?sorsolas'>
		<input type='hidden' name='action' value='sendmail' />
		".implode($form_hidden)."
		 
		".implode($form_message)."
		
		<div class='admin_button'>
			<input type='submit' class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' value='Küldés' />
		</div>
		</form>
	";

}else{
	
	$page_text .= "
	<form enctype='multipart/form-data' method='post' action='index.php?sorsolas'>
		<input type='hidden' value='sorsolas' name='action'>
		
		
		
		<div class='fieldset'>
			<div class='legend'>
				<span class='left title'>Nyertesek száma</span>
				<span class='ui-icon ui-icon-info help-box left' info-title='Nyertesek száma' info-text='Ennyi nyertest fogunk kisorsolni.'></span>
			</div>
			<div class='clear'></div>
			<input class='select ui-widget-content ui-state-default ui-corner-all' name='user_count' value='3' />
		</div>
		
		<div class='fieldset'>
			<div class='legend'>
				<span class='left title'>Teszt neve</span>
				<span class='ui-icon ui-icon-info help-box left' info-title='Teszt neve' info-text='Ebből a tesztből fog történni a sorsolás'></span>
			</div>
			<div class='clear'></div>
			<select class='select ui-widget-content ui-state-default ui-corner-all' name='test_id'>
	";
	
	$sql->mysql_select("gallery", "*");
	while($row = $sql->get_line())
	{
		$page_text .= "<option value='".$row['gallery_id']."' >".$row['gallery_name']."</option>";
	}
	
	$page_text .= "
			</select>
		</div>
		
		<div class='admin_button'>
			<input type='submit' class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' value='Sorsolás' />
		</div>
		
	</form>
	";
}
?>